#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Admin
{
	/// <summary>
	/// The Modules business object represents the modules that an application user
	/// can be granted access to within the Insight application.  It also indicates
	/// whether or not the user currently has access to each of those modules.
	/// </summary>
	public class Modules : BO.BusinessObject
	{
		#region Constants
		private const string ModulesTableName = "ModuleAccess";
		private static string ModulesKey = GenerateKey.MakeKey("Modules_F940A82C-F599-4863-8243-A1351D9C7E5B",GenerateKey.Admin);
		#endregion

		#region Public Properties
		/// <summary>
		/// Gets the DataTable that contains a users module rights details.
		/// </summary>
		public DataTable Details
		{
			get 
			{
				return DataContainer.Tables[ModulesTableName];
			}
		}

		/// <summary>
		/// Gets the DataView that contains only those modules that are granted/revoked via the UI.
		/// </summary>
		public DataView DetailsForUI
		{
			get 
			{
				DataView dataView = new DataView(DataContainer.Tables[ModulesTableName]);
				dataView.RowFilter = "IsGrantedViaUI = 1";
				return dataView;
			}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Initialize the state of this object and create commands to handle insert/delete/update.
		/// </summary>
		public Modules(DataSet dataSet, ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = dataSet;
		}

		/// <summary>
		/// Initialize the data containner.
		/// </summary>
		public Modules(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet();
		}
		#endregion

		#region Base Methods
		public override void Cache()
		{
			CachePolicy.Cache(ModulesKey, this);
		}

		public override void UnCache()
		{
			CachePolicy.UnCache(ModulesKey);
		}

		/// <summary>
		/// Loads the data container with data from repository based on the ModulesCommandType
		/// passed in.
		/// </summary>
		/// <param name="connectionString">The connection string to the data source.</param>
		/// <param name="args">arguments passed into the method.</param>
		public override void Load( string connectionString, params object[] args )
		{
			try
			{	
				if ( args.Length == 0 )
				{
					throw new Exception ("User Modules Load expects at least one argument." );
				}		
				switch ((ModulesCommandType)args[0]) 
				{
					case ModulesCommandType.GetUserModules:
						Utility.DataAccess.LoadDataSet(DataContainer, ModulesTableName, 
							connectionString, "GetUserModuleDetails",
							Utility.DataAccess.BuildSqlParameterArray("@applicationUserId", SqlDbType.Int, (int)args[1]));
						break;
					case ModulesCommandType.GetLabAccessModules:
						Utility.DataAccess.LoadDataSet(DataContainer, ModulesTableName,
							connectionString, "GetAdminLabAccessModules");
						break;
					default:
						throw new Exception("Unsupported ModulesCommandType");
				}

				if ( !DataContainer.Tables.Contains(ModulesTableName) )
					throw new Exception("Failed to load module information.");

			}
			catch( Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("Modules", e, true, Utility.ExceptionReport.ReportType.Error);
				throw e;
			}
		}
		#endregion

	}

	/// <summary>
	/// Used in Load method to determine the stored procedure to invoke.
	/// </summary>
	public enum ModulesCommandType 
	{	
		/// <summary>Gets modules a user has access to. </summary>
		GetUserModules,

		/// <summary>Gets modules used to grant people access to a lab. </summary>
		GetLabAccessModules
	}
}
